<template>
  <div class="btns">
    <el-button :icon="Plus" type="primary" size="small" @click="open(null)">新增</el-button>
    <el-popconfirm
      confirm-button-text="是"
      cancel-button-text="否"
      :icon="InfoFilled"
      icon-color="#626AEF"
      title="你确定要删除当前陪护师吗？"
      @confirm="confirmEvent"
      @cancel="cancelEvent"
    >
      <template #reference>
        <el-button :icon="Delete" type="danger" size="small">删除 </el-button>
      </template>
    </el-popconfirm>
  </div>
  <el-table :data="companionListData.list"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column prop="id" label="ID" />
    <el-table-column prop="name" label="昵称" />
    <el-table-column prop="avatar" label="头像" >
      <template #default="scope">
        <el-image style="width: 50px;height: 50px;" :src="scope.row.avatar"></el-image>
      </template>
    </el-table-column>
    <el-table-column prop="sex" label="性别">
      <template #default="scope">
        {{ scope.row.sex===1?'男':'女' }}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号" />
    <el-table-column prop="active" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.active ?'success':'danger'"> {{ scope.row.active ? '正常':'失效' }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="create_time" label="创建时间" >
      <template #default="scope">
        <div class="flex-box">
          <el-icon><Clock/></el-icon>
          <span>{{ dayjs(scope.row.create_time).format( 'YYYY-MM-DD')  }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  
  <el-dialog
    v-model="dislogFormVisible"
    title="添加陪护师"
    :before-close="beforeClose"
    width="500">
    <el-form 
      ref="formRef"
      label-width="100px"
      label-position="left"
      :model="form"
      :rules="rules"
      >
      <el-form-item v-show="false" prop="'id'">
        <el-input v-model="form.id" />
      </el-form-item>
      <el-form-item prop="name" label="姓名">
        <el-input v-model="form.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item prop="avatar" label="头像">
        <el-button v-if="!form.avatar" type="primary" @click="dialogImageVisible = true">点击上传</el-button>
        <el-image 
          v-else
          :src="form.avatar"
          style="width: 100px; height: 100px;"
         />
      </el-form-item>
      <el-form-item prop="sex" label="性别">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option value="1" label="男" />
          <el-option value="2" label="女" />
        </el-select>
      </el-form-item>
      <el-form-item prop="age" label="年龄">
        <el-input-number v-model="form.age" :min="18" :max="50" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机号码">
        <el-input v-model="form.mobile" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item prop="active" label="是否生效">
        <el-radio-group v-model="form.active">
          <el-radio :value="0">失效</el-radio>
          <el-radio :value="1">生效</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="confirm(formRef)">确认</el-button>
    </template>
  </el-dialog>

  <!-- 头像选择 -->
   <el-dialog
    v-model="dialogImageVisible"
    title="选择头像"
    :before-close="beforeImageClose"
    width="680">
      <div class="image-list">
        <div v-for="item,index in fileList" :key="item.name" class="img-box" >
          <div v-if="selectIndex === index" class="select" >
            <el-icon style="color: white;"><Check /></el-icon>
          </div>
          <el-image 
          @click="selectIndex = index"
            style="width: 148px;height: 148px;"
            :src="item.url"
          />
        </div>
      </div>
      <template #footer>
        <el-button @click="dislogImageVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmImage">确认</el-button>
    </template>
   </el-dialog>
   <!-- 分页 -->
   <div class="pagination-info">
     <el-pagination
      v-model="companionPage.pageNum"
      :page-size="companionPage.pageSize"
      :bakcground="false"
      size="small"
      layout="total,prev,pager,next"
      :total="companionListData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
     />
   </div>
</template>

<script setup>
import { nextTick, onMounted, reactive, ref } from 'vue';
import { photoList,companion, companionList,deleteCompanion }from '@/api/index.js'
import { Plus,InfoFilled, Delete } from '@element-plus/icons-vue';
import dayjs from 'dayjs';

// 获取陪护师列表数据
const companionListData = reactive({
  list:[],
  total:0
})
// 陪护师页面
const companionPage = reactive({
  pageNum:1,
  pageSize:10
})
// 处理分页大小变化
const handleSizeChange = (val)=>{
  page.pageSize = val;
  getCompanionListData();
}
// 处理当前页变化
const handleCurrentChange = (val)=>{
  page.pageNum = val;
  getCompanionListData();
}
// 获取陪护师列表数据
const getCompanionListData = ()=>{
  companionList(companionPage).then(({data})=>{
    // console.log(1)
    companionListData.list = data.data.list
    companionListData.total = data.data.total
  })
}
onMounted(()=>{
  photoList().then(({data})=>{
    fileList.value = data.data
  })
  getCompanionListData()
})

// 定义对话框可见性和关闭函数
const dislogFormVisible = ref(false);
const beforeClose = ()=>{
  dislogFormVisible.value = false
  formRef.value.resetFields()
}

const formRef = ref(null);
const form = reactive({
  id:'',
  mobile:'',
  active:1,
  age:28,
  avatar:'',
  name:'',
  sex:'1'
})

const open = (rowData = {})=>{
  dislogFormVisible.value = true
  // 如果是编辑-回显数据
  nextTick(()=>{
    if(rowData){
      Object.assign(form,rowData)
    }
  })
}

const rules = reactive({
  name:[{required:true,trigger:'blur',message:'请输入昵称'}],
  avatar:[{required:true,message:'请选择头像'}],
  sex:[{required:true,trigger:'blur',message:'请选择性别'}],
  mobile:[{required:true,trigger:'blur',message:'请填写手机号码'}]
})

const confirm = async(formEl)=>{
  if(!formEl) return
  await formEl.validate((valid,fields)=>{
    if(valid){
      // console.log(form.value)
      companion(form).then(({data})=>{
        if(data.code===10000){
          ElMessage.success(data.data)
          beforeClose()
          getCompanionListData()
        }else{
          ElMessage.error(data.message)
        }
      })
    }else{
      console.log('error submint!',fields)
    }
  })
}

// 打开头像对话框
const dialogImageVisible = ref(false)

// 获取头像列表
const fileList = ref([])

const beforeImageClose=()=>{
  dialogImageVisible.value = false
}

// 选中头像
const selectIndex = ref(0)

const confirmImage=()=>{
  form.avatar = fileList.value[selectIndex.value].url
  dialogImageVisible.value = false
}

//选择选中的数据
const selectTableData = ref([])
// 全选事件
const handleSelectionChange = (val)=>{
  const arr = val.map(item =>({id:item.id}))
  // console.log(arr)
  selectTableData.value = arr
}
const cancelEvent=()=>{}

// 删除事件
const confirmEvent = ()=>{
  if(!selectTableData.value.length){
    return ElMessage.warning('请选择至少一项')
  }
  deleteCompanion({id:selectTableData.value}).then(({data})=>{
    if(data.code===10000){
      ElMessage.success(data.data)
      getCompanionListData()
    }
  })
  
}
</script>

<style lang="less" scoped>
.flex-box {
  display: flex;
  align-items: center;
  span{
    margin-left: 5px;
  }
}
.btns{
  padding: 10px 0 10px 10px;
  background-color: white;
}
.image-list{
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .img-box{
    position: relative;
    .select{
      position: absolute;
      left: 0;
      top: 0;
      width: 24px;
      height: 24px;
      background-color: #67c23a;
      z-index: 999;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .el-image{
    margin-right: 10px;
    margin-bottom: 10px;
  }
}
</style>